package com.gitee.wsl.common.ui.ext.path

import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.RoundRect
import androidx.compose.ui.graphics.Path


sealed class RadiusPx {
    data object None : RadiusPx()
    data class Circular(val radius: Float) : RadiusPx()
    data class Rectangle(
        val topLeft: Float = 0f,
        val topRight: Float = 0f,
        val bottomLeft: Float = 0f,
        val bottomRight: Float = 0f
    ) : RadiusPx()

    fun reverse(horizontal:Boolean = false):RadiusPx{
        return when(this){
            is RadiusPx.Circular, is RadiusPx.None ->{
                this
            }
            is RadiusPx.Rectangle ->{
                if (horizontal){
                    copy(
                        topLeft = topRight,
                        topRight = topLeft,
                        bottomLeft = bottomRight,
                        bottomRight = bottomLeft
                    )
                }else{
                    copy(
                        topLeft = bottomLeft,
                        topRight = bottomRight,
                        bottomLeft = topLeft,
                        bottomRight = topRight
                    )
                }
            }
        }
    }
}


fun Path.addRoundRect(
    rect: Rect,
    radius: RadiusPx
){
    when (radius) {
        is RadiusPx.None -> {
            addRect(rect)
        }

        is RadiusPx.Circular -> {
            addRoundRect(
                roundRect = RoundRect(
                    rect = rect,
                    cornerRadius = CornerRadius(
                        x = radius.radius,
                        y = radius.radius
                    )
                )
            )
        }

        is RadiusPx.Rectangle -> {
            addRoundRect(
                roundRect = RoundRect(
                    rect = rect,
                    topLeft = CornerRadius(
                        x = radius.topLeft,
                        y = radius.topLeft
                    ),
                    topRight = CornerRadius(
                        x = radius.topRight,
                        y = radius.topRight
                    ),
                    bottomLeft = CornerRadius(
                        x = radius.bottomLeft,
                        y = radius.bottomLeft
                    ),
                    bottomRight = CornerRadius(
                        x = radius.bottomRight,
                        y = radius.bottomRight
                    ),
                )
            )
        }
    }
}